*************************************************************************************
* Author: Guy Grossman 																
* This version's date: 08/23/2013													
* Project: Do Selection Rules Affect Leader Responsiveness? \\Evidence from Rural Uganda
*************************************************************************************
capture log close
clear all
set more off
macro drop _all
version 12.1

**********************************************************************************
cd "/Users/guy/Dropbox/Dissertation/Voting Rule/QJPS/replication files/"
log using DC_analysis, replace

use "DC_data.dta"

********************************************************
* Generate summary indices using Anderson (2008)
********************************************************

* run program that creates indices based on Anderson (2008)
do "genindex.do"

* Other governence rules summary index
alpha dscq1 dscq20 dscq21 dscq22a dscq5 dscq8 dseq87 dseq94 dscq4a dscq16, item st
genindex dscq1 dscq20 dscq21 dscq22a dscq5 dscq8 dseq87 dseq94 dscq4a dscq16, nv(governance)

* Leader responsiveness summary index
alpha imsf30b_memb irsf19b_reps ss_warned2_memb imse11_memb imsf28_d4_memb, st item 
genindex imsf30b_memb irsf19b_reps ss_warned2_memb imse11_memb imsf28_d4_memb, nv(responsiveness)

* Members' cooperation summary index
alpha isd28_memb isd18_memb isd20_memb contribution_memb imbulk_a_memb isb13_memb imse09b_memb, st item
genindex isd28_memb isd18_memb isd20_memb contribution_memb imbulk_a_memb isb13_memb imse09b_memb, nv(cooperation) 

* Monitoring summary index
alpha nummeetings irsf10_reps irsf20b_reps irsf21_reps isd15_reps isd15_memb sf222_reps imsf32_d2_memb imsf33_memb, st item
genindex nummeetings irsf10_reps irsf20b_reps irsf21_reps isd15_reps isd15_memb sf222_reps imsf32_d2_memb imsf33_memb, nv(monitoring)

* Leader network centrality summary index
alpha m_indegree_friend m_outdegree_friend m_closeness_friend m_eigenvector_friend, st item
genindex m_indegree_friend m_outdegree_friend m_closeness_friend m_eigenvector_friend, nv(friend)

* Manager's ability summary index
alpha m_isj03 m_isj04 m_isj06b m_raven, st item
genindex m_isj03 m_isj04 m_isj06b m_raven, nv(mability)

* Set globals
global controls npo dcage c_dsbq8b
global governance governanceA dscq1 dscq20 dscq21 dscq22a dscq5 dscq8 dseq87 dseq94 dscq4a dscq16
global responsiveness responsivenessA imsf30b_memb irsf19b_reps ss_warned2_memb imse11_memb imsf28_d4_memb
global cooperation cooperationA isd28_memb isd18_memb isd20_memb contribution_memb imbulk_a_memb isb13_memb imse09b_memb
global services dsdq64 dsdq69 dsdq79 standprice
global monitoring monitoringA nummeetings irsf10_reps irsf20b_reps irsf21_reps isd15_reps isd15_memb sf222_reps imsf32_d2_memb imsf33_memb
global centrality friendA m_indegree_friend m_outdegree_friend m_closeness_friend m_eigenvector_friend
global observables mabilityA m_isj03 m_isj04 m_isj06b m_raven m_age m_church m_isb06 manager_wealth
global dictator stranger pomember group_bias
global TPP tpp4 tpp3 tpp2 tpp1 tpp0

********************************************************
* Balance test: farmer-association level (DC) data (online appendix, table 3) 
********************************************************
foreach var in dsbq8b agedc npos_before nmembers_before{
	ttest `var', by(z) unp
	reg `var' z
}


**********************************************************************************
* Pre-treatment Balance Across Treatment Assignment (Online appendix, Table 3)
**********************************************************************************

foreach var in $governance{
ttest `var', by(z) unp
}

**********************************************************************************
* Balance test: "other" governance institutions (online appendix, table 4) 
**********************************************************************************

* Calculate false discovery rates for multiple comparisons
preserve
        global tflist ""
        global modseq=0
        foreach var in $governance{
        global modseq=$modseq+1
        tempfile tfcur
        parmby "reg `var' z, r", label for(estimate stderr min90 max90 p %8.3f) li(parm label estimate min90 max90 p, clean noobs) level(90) idn($modseq) saving(`"`tfcur'"', replace) flist(tflist)
        }        
        drop _all
        append using $tflist
        sort idnum parmseq
        drop if parm=="_cons"
        drop parmseq
        ren stderr se
        describe
        lab define governance 1 "Governance summary index" 2 "Have a signed constitution" 3 "Frequency council meetings (on-season)" 4 "Frequency council meetings (off-season)" 5 "Council meeting frequency codified in constitution" 6 "Frequency executives meetings (on-season)" 7 "Frequency executives meetings (off-season)" 8 "DC council chairman term length" 9 "DC Manager term length" 10 "Manager attends executive meetings regularly" 11 "Council representatives can hold other PO positions", modify         
        lab value idnum governance
        qqvalue p, method(simes) qvalue(myqval)
        by idnum :list parm label estimate se min90 max90 p myqval, noobs
 restore

**********************************************************************************
* Field Trainer Recommendation (online appendix, Table 2 - four right columns)
* Data here is only on sampled DCs
**********************************************************************************
tabulate dsbq9b z
tabulate dsbq9b vrule

**********************************************************************************
**********************************************************************************
**********************************************************************************
* Parametric Regressions Analysis (ITT and IV)
* 1. ITT with no control
* 2. ITT with group controls
* 3. IV with no control
* 4. IV with group controls

* All outcomes are standardized (mean=0), in units of the control
* Control variables are centered 
**********************************************************************************
**********************************************************************************
**********************************************************************************

**********************************************************************************
* Leader responsivness (table 1, top panel)
**********************************************************************************
* standardize variables that make up the index
foreach var in imsf30b_memb irsf19b_reps ss_warned2_memb imse11_memb imsf28_d4_memb{
	qui sum `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regerssions	
foreach var in responsivenessA z_imsf30b_memb z_irsf19b_reps z_ss_warned2_memb z_imse11_memb z_imsf28_d4_memb{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

* prepare a small dataset of ITT with no controls to graph in R
preserve
        global tflist ""
        global modseq=0
        foreach var in responsivenessA z_imsf30b_memb z_irsf19b_reps z_ss_warned2_memb z_imse11_memb z_imsf28_d4_memb {
        global modseq=$modseq+1
        tempfile tfcur
        parmby "qui reg `var' z, robust", label for(estimate stderr min90 max90 p %8.3f) li(parm label estimate min90 max90 p, clean noobs) level(90) idn($modseq) saving(`"`tfcur'"', replace) flist(tflist)
        }        
        drop _all
        append using $tflist
        sort idnum parmseq
        drop if parm=="_cons"
        drop parmseq
        ren stderr se
        describe
        lab define responsiveness 1 "Responsiveness summary index" 2 "Member obtained receipts (M)" 3 "Receipts given to members  (R)" 4 "Member warned: side-selling" 5 "Member warned: bad agri practices" 6 "Manager is very transparency", modify         
        lab value idnum responsiveness
        by idnum :list parm label estimate se min90 max90 p, noobs
 		save "temp/responsiveness.dta", replace
restore

**********************************************************************************
* Member Cooperation (table 1, second panel)
**********************************************************************************	

* create a var that captures membership body growth rate since the foundation of the group
* note that growth is not part of the cooperation summary index
gen growth = ((nmembers_after/nmembers_before)^(1/(agedc - 1))) -1 
replace growth = (nmembers_after-nmembers_before)/nmembers_before if agedc==1 

* standardize variables that make up the index
foreach var in isd28_memb isd18_memb isd20_memb contribution_memb imbulk_a_memb isb13_memb imse09b_memb growth{
	qui sum `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regerssions
foreach var in cooperationA z_isd28_memb z_isd18_memb z_isd20_memb z_contribution_memb z_imbulk_a_memb z_isb13_memb z_imse09b_memb z_growth{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

* prepare small dataset of ITT with no controls to graph in R
preserve
        global tflist ""
        global modseq=0
        foreach var in cooperationA z_isd28_memb z_isd18_memb z_isd20_memb z_contribution_memb z_imbulk_a_memb z_isb13_memb z_imse09b_memb {
        global modseq=$modseq+1
        tempfile tfcur
        parmby "qui reg `var' z, robust", label for(estimate stderr min90 max90 p %8.3f) li(parm label estimate min90 max90 p, clean noobs) level(90) idn($modseq) saving(`"`tfcur'"', replace) flist(tflist)
        }        
        drop _all
        append using $tflist
        sort idnum parmseq
        drop if parm=="_cons"
        drop parmseq
        ren stderr se
        describe
        lab define cooperation 1 "Cooperation summary index" 2 "Member agree to increase DC commission" 3 "Member paid joining fees" 4 "Member paid annual dues (past season)" 5 "Member's contribution: commitment experiment" 6 "Member's share of coffee bulked" 7 "Member planted seedlings (past season)" 8 "Member dried coffee on tarps (past season)", modify         
        lab value idnum cooperation
        by idnum :list parm label estimate se min90 max90 p, noobs
 		save "temp/cooperation.dta", replace
restore

********************************************************************************** 
* Services that the DC provides to it's members (table 1, third panel)
********************************************************************************** 

* standardize variables
foreach var in dsdq64 dsdq69 dsdq79 standprice {
	qui su `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regerssions
foreach var in z_dsdq64 z_dsdq69 z_dsdq79 z_standprice{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

**********************************************************************************
* DC level of monitoring (table 2, top panel)
**********************************************************************************

* standardize the variables that make up the monitoring summary index
foreach var in nummeetings irsf10_reps irsf20b_reps irsf21_reps isd15_reps isd15_memb sf222_reps imsf32_d2_memb imsf33_memb{
	qui sum `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}
	
* run ITT and IV parametric regerssions
foreach var in monitoringA z_nummeetings z_irsf10_reps z_irsf20b_reps z_irsf21_reps z_isd15_reps z_isd15_memb z_sf222_reps z_imsf32_d2_memb z_imsf33_memb{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

* prepare small dataset of ITT with no controls to graph in R
preserve
        global tflist ""
        global modseq=0
        foreach var in monitoringA z_nummeetings z_irsf10_reps z_irsf20b_reps z_irsf21_reps z_isd15_reps z_isd15_memb z_sf222_reps z_imsf32_d2_memb z_imsf33_memb{
        global modseq=$modseq+1
        tempfile tfcur
        parmby "qui reg `var' z, robust", label for(estimate stderr min90 max90 p %8.3f) li(parm label estimate min90 max90 p, clean noobs) level(90) idn($modseq) saving(`"`tfcur'"', replace) flist(tflist)
        }        
        drop _all
        append using $tflist
        sort idnum parmseq
        drop if parm=="_cons"
        drop parmseq
        ren stderr se
        describe
        lab define monitoring 1 "Monitoring summary index" 2 "DC council meeting frequency (E)" 3 "Reps influence over decisions (R)" 4 "External auditing (R)" 5 "Internal auditing (R)" 6 "General assembly attendance (R)" 7 "General assembly attendance (M)" 8 "Manager is monitored  (R)" 9 "Manager is monitored  (M)" 10 "Manager is accountable (M)", modify         
        lab value idnum monitoring
        by idnum :list parm label estimate se min90 max90 p, noobs
 		save "temp/monitoring.dta", replace
restore

* Test for bottom-up grassroots participation
foreach var in isd15_memb isd06a_memb isd06b_memb isd06c_memb isd12a_memb isd12b_memb isd12c_memb {
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var' z $controls, robust
	esttab `var'_m*, b(3) se(3) keep(z) nostar
   }

**********************************************************************************
* Manager's Network Centrality (table 2, bottom panel)
**********************************************************************************

* standardize variables that make up the network centrality index
foreach var in m_indegree_friend m_outdegree_friend m_closeness_friend m_eigenvector_friend {
	qui su `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regerssions
foreach var in  friendA z_m_indegree_friend z_m_outdegree_friend z_m_closeness_friend z_m_eigenvector_friend {
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

* prepare a small dataset of ITT with no controls to graph in R
preserve
        global tflist ""
        global modseq=0
        foreach var in friendA z_m_indegree_friend z_m_outdegree_friend z_m_closeness_friend z_m_eigenvector_friend {
        global modseq=$modseq+1
        tempfile tfcur
        parmby "qui reg `var' z, robust", label for(estimate stderr min90 max90 p %8.3f) li(parm label estimate min90 max90 p, clean noobs) level(90) idn($modseq) saving(`"`tfcur'"', replace) flist(tflist)
        }        
        drop _all
        append using $tflist
        sort idnum parmseq
        drop if parm=="_cons"
        drop parmseq
        ren stderr se
        describe
        lab define network 1 "Network centrality summary index" 2 "Indegree centrality" 3 "Outdegree centrality" 4 "Total degree centrality" 5 "Closeness centrality" 6 "Eigenvector centrality", modify         
        lab value idnum network
        by idnum :list parm label estimate se min90 max90 p, noobs
 		save "temp/network.dta", replace
restore

**********************************************************************************
* Manager's observable characteristics (table 3, top panel)
**********************************************************************************

* standardize variables that make up the ability index
foreach var in m_isj03 m_isj04 m_isj06b m_raven{
	qui su `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}
* run ITT and IV parametric regerssions
foreach var in mabilityA z_m_isj03 z_m_isj04 z_m_isj06b z_m_raven{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

* run ITT and IV regerssions on other observable charecterstics
foreach var in m_age m_church m_isb06 manager_wealth{
	eststo `var'_m1: reg `var' z, robust
	eststo `var'_m2: reg `var'  z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

**********************************************************************************
* Dictator game (table 3, bottom panel)
**********************************************************************************
* gen a var that captures the difference between allocation to a group member and a starnger
	gen group_bias= pomember-stranger 
	tabstat stranger pomember group_bias, by(vrule) stats (mean sd N) f(%5.2f) 
	tabstat stranger pomember group_bias, by(z) stats (mean N) f(%5.2f) 

* standardize variables
foreach var in stranger pomember group_bias{
	qui sum `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regressions
foreach var in z_stranger z_pomember z_group_bias{
	eststo `var'_m1: reg `var'  z, robust
	eststo `var'_m2: reg `var' z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

**********************************************************************************
* Third party punishment (table 3, bottom panel)
**********************************************************************************

* standardize variables capturing whether manager punsihes "unfair" allocations
foreach var in tpp4 tpp3 tpp2 tpp1{
	qui sum `var' if z==0, de
	global `var'mean = r(mean)
	global `var'sd = r(sd)
	gen z_`var'=(`var'-$`var'mean)/$`var'sd
	}

* run ITT and IV parametric regressions
foreach var in z_tpp4 z_tpp3 z_tpp2 z_tpp1{
	eststo `var'_m1: reg `var'  z, robust
	eststo `var'_m2: reg `var' z $controls, robust
	eststo `var'_m3: ivreg2 `var' (vrule=z), gmm2s robust
	eststo `var'_m4: ivreg2 `var' (vrule=z) $controls, gmm2s robust
	esttab `var'_m*, b(3) se(3) keep(z vrule) nostar
   }

********************************************************************************** 
********************************************************************************** 
********************************************************************************** 
* Randomization inference (ITT)
* 1. randomization takes place at the group level (reported in paper)
* 2. randomization takes place at the trainer level (robustness)
********************************************************************************** 
********************************************************************************** 
********************************************************************************** 

* define outcome variables
# delimit 
global y z_stranger z_pomember z_group_bias z_tpp4 z_tpp3 z_tpp2 z_tpp1
mabilityA z_m_isj03 z_m_isj04 z_m_isj06b z_m_raven
friendA z_m_indegree_friend z_m_outdegree_friend z_m_closeness_friend z_m_eigenvector_friend
monitoringA z_nummeetings z_irsf10_reps z_irsf20b_reps z_irsf21_reps z_isd15_reps z_isd15_memb z_sf222_reps z_imsf32_d2_memb z_imsf33_memb
z_dsdq64 z_dsdq69 z_dsdq79 z_standprice
cooperationA z_isd28_memb z_isd18_memb z_isd20_memb z_contribution_memb z_imbulk_a_memb z_isb13_memb z_imse09b_memb z_growth
responsivenessA z_imsf30b_memb z_irsf19b_reps z_ss_warned2_memb z_imse11_memb z_imsf28_d4_memb 
isd06b_memb isd12b_memb m_age m_church m_isb06 manager_wealth;
#delimit cr

********************************************************************************** 
* 1. randomization takes place at the group level (reported in paper)
********************************************************************************** 
* program to conduct RI --> 
    cap program drop mydiff
	program mydiff, rclass
	  args y group
      su `y' if `group'==1, meanonly
      local mean_treat = r(mean)
      su `y' if `group'==0
      return scalar diff = `mean_treat' -  r(mean)
	end

* execute program --> 
	foreach var in $y{
	set seed 20111102
	tsrtest z r(diff), reps(10000): mydiff `var' z   
	global p_`var'=r(twotail)
	global max_`var'= r(uppertail) 
	global min_`var'= r(lowertail)  
	global theta_`var'= r(obsvStat)
	}

**********************************************************************************
* 2. randomization takes place at the trainer level (robustness)
**********************************************************************************

preserve
collapse (count) n=responsivenessA (mean) z $y, by(dsbq9b)

* updated program to conduct RI --> 
cap program drop mydiff
program mydiff, rclass
	  args y w group
      su `y' [w= `w'] if `group'==1, meanonly
      local mean_treat = r(mean)
      su `y' [w= `w'] if `group'==0
      return scalar diff = `mean_treat' -  r(mean)
end

* execute updated program --> 
foreach var in $y {
tsrtest z r(diff): mydiff `var' n z
global p_`var'=r(twotail)
global max_`var'= r(uppertail) 
global min_`var'= r(lowertail)  
global theta_`var'= r(obsvStat)
}

restore

**********************************************************************************
**********************************************************************************
**********************************************************************************
* Generate Figures
**********************************************************************************
**********************************************************************************
**********************************************************************************


**********************************************************************************
* graph relationship between members' cooperation and manager responsiveness
**********************************************************************************

# delimit 
graph twoway (lfitci cooperationA responsivenessA, clwidth(thick)) 
             (scatter cooperationA responsivenessA, ms(oh)) 
             ,ylabel(, labsize(3)) scheme(lean1) 
              ytitle(Members' Cooperation Summary Index, size(3.5)) 
              xtitle(Leader Responsiveness Summary Index, size(3.5)) 
              legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) 
              xlabel(-1.5(.5)1.5, labsize(3)) note("N=50", size(3));
			  #delimit cr
			  
**********************************************************************************
* graph relationship between members' cooperation and monitoring level
**********************************************************************************

# delimit 
graph twoway (lfitci cooperationA monitoringA, clwidth(thick)) 
             (scatter cooperationA monitoringA, ms(Oh))
             ,ylabel(-1.5(.5)1.5, labsize(3)) yscale(r(-1.5 1.5)) xlabel(-1.5(.5)1.5, labsize(3)) xscale(r(-1.5 1.5))  
              ytitle(Members' Cooperation Summary Index, size(3.5)) xtitle(Monitoring Summary Index, size(3.5))
              legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) 
              note("N=50", size(3)) scheme(lean1);
			  #delimit cr
                            

**********************************************************************************
* relationship between frisndship network centrality and monitoring index (figure 5)
**********************************************************************************

fracpoly reg monitoringA friendA
fracplot, msymbol() title("") scheme(lean1) yscale(r(-2 2)) ylab(-2(1)2) ytitle("Monitoring Summary Index", size(3.5)) xtitle("Friendship Network Centrality", size(3.5))  lineopts(lp(solid) lc(red)) 

**********************************************************************************
* relationship between monitoring level and leader responsivness (Figure 3)
**********************************************************************************

# delimit 
graph twoway (lfitci responsivenessA monitoringA, clwidth(thick))
             (scatter responsivenessA monitoringA, ms(Oh)) 
             ,ylabel(-1.5(.5)1.5, labsize(3)) yscale(r(-1.5 1.5)) 
              xlabel(-1.5(.5)1.5, labsize(3)) xscale(r(-1.5 1.5))  
              ytitle(Responsiveness Summary Index, size(3.5)) 
              xtitle(Monitoring Summary Index, size(3.5)) 
              legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) 
              note("N=50", size(3)) scheme(lean1);
			  #delimit cr

**********************************************************************************
* Dictator game: predicted probabilities (figure 6)
**********************************************************************************

* 1. run IV regerssion with controls and predict allocation
foreach var in stranger pomember group_bias{
	ivreg2 `var' (vrule=z) $controls, gmm2s robust
	margins, over(vrule) post
	parmest, label format(estimate stderr min* max* %8.2f p %8.3f) list(parm estimate min* max* stderr p) saving("temp/PredicProb.`var'.dta", replace)
	}

* 2. graph predicted probabilities
cd "/Users/guy/Dropbox/Dissertation/Voting Rule/QJPS/replication files/"
preserve 
	use "temp/PredicProb.stranger.dta", clear
	append using "temp/PredicProb.pomember.dta"
	append using "temp/PredicProb.group_bias.dta"

	replace parm="0" if parm=="0.vrule"
	replace parm="1" if parm=="1.vrule"
	destring parm, replace
	lab define parm 0 "Appointments" 1 "Direct elections" 
	lab	value parm parm

	gen model=1
	replace model=2 in 3/4
	replace model=3 in 5/6
	lab define model 1 "Stranger" 2 "Member" 3 "Member-Stranger"
	lab value model model

# delimit 
eclplot estimate min95 max95 model, eplot(bar) ciopts(blcolor(black)) estopts(barwidth(0.25)) estopts1(bcolor(black)) 
       estopts2(bcolor(gs7)) supby(parm, spaceby(0.25)) xscale(range(1(1)3)) 
 	   title("Leader Allocation", size(5)) subtitle("Dictator Game (IV estimates)")
	   legend(row(2) position(10) ring(0) region(style(outline))) 
       xlabel(1.125 "Stranger" 2.125 "DC Member" 3.125 "Member-Stranger")  yscale(r(0 800)) ylabel(0 "0" 200 "200" 400 "400" 600 "600" 800 "800") 
       xtitle("") ytitle("Predicted Allocation", size(4)) scheme(lean1);
	   #delimit cr
restore

**********************************************************************************
**********************************************************************************
**********************************************************************************
* Descriptive stastitics tables (table 4)
**********************************************************************************
**********************************************************************************
**********************************************************************************
lab var governanceA "Other governance rules sumamry index"
lab var responsivenessA "Responsiveness summary index"
lab var cooperationA "Cooperation summary index"
lab var monitoringA "Monitoring summary index"
lab var friendA "Network centrality summary index"
lab var group_bias "Member-stranger allocation (DG)"

estpost sum npos_before	dsbq8b agedc $governance $responsiveness $cooperation $services growth $monitoring $centrality $observables $dictator $TPP
esttab, replace cells("mean(fmt(a3)) sd(fmt(a3)) min(fmt(a3)) max(fmt(a3)) count") noobs label

**********************************************************************************
* Save dataset of standardized outcomes for robsutness check in R
**********************************************************************************
/*
keep agedc npos_before dsbq8b vrule z z_group_bias friendA monitoringA z_irsf10_reps z_irsf20b_reps z_irsf21_reps z_isd15_reps z_isd15_memb z_sf222_reps z_imsf32_d2_memb z_imsf33_memb cooperationA z_isd28_memb z_isd18_memb z_isd20_memb z_contribution_memb z_imbulk_a_memb z_isb13_memb z_imse09b_memb responsivenessA z_imsf30b_memb z_irsf19b_reps z_ss_warned2_memb z_imse11_memb z_imsf28_d4_memb isd06b_memb isd12b_memb
move  dsbq8b isd06b_memb
move  agedc isd06b_memb
move  npos_before isd06b_memb
move z isd06b_memb
move vrule isd06b_memb
save "/Users/guy/Dropbox/Dissertation/Voting Rule/QJPS/replication files/GenMatch.dta", replace
*/

capture log close
exit
